package com.rop.znyx.product.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rop.znyx.common.result.Result;
import com.rop.znyx.model.product.Category;
import com.rop.znyx.product.service.CategoryService;
import com.rop.znyx.vo.product.CategoryQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品三级分类 前端控制器
 * </p>
 *
 * @author rop
 * @since 2023-06-24
 */
@Api(tags = "商品分类管理")
@RestController
@RequestMapping(value = "admin/product/category")
@RequiredArgsConstructor

public class CategoryController {

    private final CategoryService categoryService;

    /**
     * 获取商品分类列表
     */
    @ApiOperation("获取商品分类列表")
    @GetMapping("{page}/{limit}")
    public Result<IPage<Category>> index(
            @ApiParam(name = "page", value = "当前页码", required = true) @PathVariable("page") Long page,
            @ApiParam(name = "limit", value = "每页条数", required = true) @PathVariable("limit") Long limit,
            @ApiParam(name = "categoryQueryVo", value = "查询条件", required = true) CategoryQueryVo categoryQueryVo) {

        Page<Category> pageParam = new Page<>(page, limit);

        IPage<Category> categoryIPage = categoryService.selectByPage(pageParam, categoryQueryVo);

        return Result.ok(categoryIPage);
    }

    /**
     * 根据id获取商品分类信息
     */
    @ApiOperation("根据id获取商品分类信息")
    @GetMapping("get/{id}")
    public Result<Category> get(@PathVariable("id") Long id) {
        Category category = categoryService.getById(id);
        return Result.ok(category);
    }

    /**
     * 新增商品分类
     */
    @ApiOperation(value = "新增商品分类")
    @PostMapping("save")
    public Result<Boolean> save(@RequestBody Category category) {
        boolean isSuccess = categoryService.save(category);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 修改商品分类
     */
    @ApiOperation(value = "修改商品分类")
    @PutMapping("update")
    public Result<Boolean> updateById(@RequestBody Category category) {
        boolean isSuccess = categoryService.updateById(category);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 删除商品分类
     */
    @ApiOperation(value = "删除商品分类")
    @DeleteMapping("remove/{id}")
    public Result<Boolean> remove(@PathVariable Long id) {
        boolean isSuccess = categoryService.removeById(id);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 根据id列表删除商品分类
     */
    @ApiOperation(value = "根据id列表删除商品分类")
    @DeleteMapping("batchRemove")
    public Result<Boolean> batchRemove(@RequestBody List<Long> idList) {
        boolean isSuccess = categoryService.removeByIds(idList);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 获取全部商品分类
     */
    @ApiOperation(value = "获取全部商品分类")
    @GetMapping("findAllList")
    public Result<List<Category>> findAllList() {
        return Result.ok(categoryService.findAllList());
    }

}

